package com.xijia.wuyouwallet.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

/**
 * Created by Administrator on 2017/11/29 0029.
 *
 * picasso 图片处理工具类<BR/>
 * 优缺点：对图片处理强大，取消已经不在视野范围的ImageView图片资源的加载（否则会导致图片错位），
 * 使用4.0+系统上的HTTP缓存来代替磁盘缓存；只能得到结果，不能监听图片下载过程
 * <BR/> Picasso 可以与okhttp搭配
 *
 * 如果使用Picasso同时也使用了okhttp库，那么项目运行的时候可能会报出一下异常：
 Picasso detected an unsupported OkHttp on the classpath
 针对该情况，网上说需要引入：compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
 即同时引入一下三个包：
 compile 'com.squareup.okhttp:okhttp:2.4.0'
 compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
 compile 'com.squareup.picasso:picasso:2.4.0'
 *
 */

public class PicassoUtils {

    /**圆形*/
    public static String PICASSO_BITMAP_SHOW_CIRCLE_TYPE="PicassoUtils_Circle_Type";
    /**圆角*/
    public static String PICASSO_BITMAP_SHOW_ROUND_TYPE="PicassoUtils_Round_Type";
    /**正常*/
    public static String PICASSO_BITMAP_SHOW_NORMAL_TYPE="PicassoUtils_Normal_Type";

    private static PicassoUtils instance;

    public static PicassoUtils getinstance(){
        if(instance==null){
            synchronized (PicassoUtils.class) {
                if(instance==null){
                    instance=new PicassoUtils();
                }
            }
        }
        return instance;
    }

    /*public void LoadImage(Context context, String path, ImageView imageView, int placeholderimage, int errorimage, String bitmapShowType, float roundRadius){
        if(bitmapShowType.equals(PICASSO_BITMAP_SHOW_CIRCLE_TYPE)){
            Picasso.with(context).load(path).placeholder(placeholderimage).error(errorimage).transform(new CircleTransform()).into(imageView);
        }else if(bitmapShowType.equals(PICASSO_BITMAP_SHOW_ROUND_TYPE)){
            Picasso.with(context).load(path).placeholder(placeholderimage).error(errorimage).transform(new RoundTransform(roundRadius)).into(imageView);
        }else {
            Picasso.with(context).load(path).placeholder(placeholderimage).error(errorimage).into(imageView);
        }
    }*/

    /**
     * 加载有默认图片
     *
     * @param mContext
     * @param path
     * @param resId
     * @param mImageView
     * @param errorimage
     */
    public void loadImageViewHolder(Context mContext, boolean isFit, String path, int resId, int errorimage, ImageView mImageView){
        if (isFit){
            Picasso.with(mContext).load(path).fit().placeholder(resId).error(errorimage).into(mImageView);
        }else {
            Picasso.with(mContext).load(path).placeholder(resId).error(errorimage).into(mImageView);
        }
    }


    /**
     * 指定大小加载图片
     *
     * @param mContext   上下文
     * @param path       图片路径
     * @param width      宽
     * @param height     高
     * @param mImageView 控件
     */
    public void loadImageViewSize(Context mContext, String path, int width, int height, ImageView mImageView) {
        Picasso.with(mContext).load(path).resize(width, height).centerCrop().into(mImageView);
    }


    /**
     * 裁剪图片
     *
     * @param mContext   上下文
     * @param path       图片路径
     * @param mImageView 控件
     */
    public void loadImageViewCrop(Context mContext, String path, ImageView mImageView) {
        Picasso.with(mContext).load(path).transform(new CropImageView()).into(mImageView);
    }

    /**
     * 自定义图片裁剪
     */
    public  class CropImageView implements Transformation {

        @Override
        public Bitmap transform(Bitmap source) {
            int size = Math.min(source.getWidth(), source.getHeight());
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;

            Bitmap newBitmap = Bitmap.createBitmap(source, x, y, size, size);

            if (newBitmap != null) {
                //内存回收
                source.recycle();
            }
            return newBitmap;
        }

        @Override
        public String key() {

            return "lgl";
        }
    }


}
